﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class frm_MOOptionAddThis : DevExpress.XtraEditors.XtraForm
    {
        private string minDate = "1/1/1753 12:00:00 AM";

        public string ID;
        public string mONo;
        public string partID;
        public string partIDNew;
        //public string machineID;
        //public string optionID;
        //public string lastOption;
        //public string jigType;
        //public string finished;
        //public string finishdate;
        //public string shift;
        public string mOQty;
        //public string proTime;
        //public string clampTime;
        public string deadline;
        //public string note;
        DataTable dtCm = new DataTable();
        private LogUtils log;
        DataTable dtOp = new DataTable();
        DataTable dtMo = new DataTable();

        public frm_MOOptionAddThis()
        {
            InitializeComponent();
            log = new LogUtils();
            //this.cbOptionID.LostFocus += cbOptionID_LostFocus;
        }

        private void frm_MOOptionEdit_Load(object sender, EventArgs e)
        {
            //danh sách MONo
            //DataTable dtMONo = new DataTable();
            //dtMONo = clsMODAO.GetAllMO();
            //DataRow rMONo = dtMONo.NewRow();
            //rMONo["MONo"] = "";
            //dtMONo.Rows.InsertAt(rMONo, 0);
            //cbMONo.DataSource = dtMONo;
            //cbMONo.DisplayMember = "MONo";
            //cbMONo.ValueMember = "MONo";
            //List<ClsMOOptionTable> objs = new List<ClsMOOptionTable>();
            //ClsMOOptionTable obj1 = new ClsMOOptionTable();
            //obj1.MONo = "";
            //obj1.PartID = "";
            //objs.Add(obj1);
            //foreach (DataRow r in dtMONo.Rows)
            //{
            //    ClsMOOptionTable obj = new ClsMOOptionTable();
            //    obj.MONo = r["MONo"].ToString();
            //    obj.PartID = r["PartID"].ToString();
            //    objs.Add(obj);
            //}
            //cbMONo.DataSource = objs;
            //cbMONo.DisplayMember = "MONo";
            //cbMONo.ValueMember = "MONo";

            //danh sách PartID
            //DataTable dtPartID = new DataTable();
            //dtPartID = clsMODAO.GetAllPartID();
            //DataRow rPartID = dtPartID.NewRow();
            //rPartID["PartNo"] = "";
            ////rPartID["PartID"] = "";
            //dtPartID.Rows.InsertAt(rPartID, 0);
            //cbPartID.DataSource = dtPartID;
            //cbPartID.DisplayMember = "PartNo";
            //cbPartID.ValueMember = "PartNo";


            ////danh sách OptionID
            //DataTable dtOptionID = new DataTable();
            //dtOptionID = clsMODAO.GetAllOptionID();
            //DataRow rOptionID = dtOptionID.NewRow();
            //rOptionID["OptionID"] = "";
            ////rPartID["PartID"] = "";
            //dtOptionID.Rows.InsertAt(rOptionID, 0);
            //cbOptionID.DataSource = dtOptionID;
            //cbOptionID.DisplayMember = "OptionID";
            //cbOptionID.ValueMember = "OptionID";

            ////danh sách MachineID
            //DataTable dtMachineID = new DataTable();
            //dtMachineID = clsMODAO.GetAllMachineID();
            //DataRow rMachineID = dtMachineID.NewRow();
            //rMachineID["MachineID"] = "";
            ////rPartID["PartID"] = "";
            //dtMachineID.Rows.InsertAt(rMachineID, 0);
            //cbMachineID.DataSource = dtMachineID;
            //cbMachineID.DisplayMember = "MachineID";
            //cbMachineID.ValueMember = "MachineID";

            //đưa giá trị vào danh sách
            cbMONo.Text = mONo;
            cbPartID.Text = partID;
            txtMOQty.Text = mOQty;
            dateDeadline.DateTime = DateTime.Parse(deadline);
            partIDNew = partID;
            if (partID.Contains("+"))
            {
                partIDNew = partID.Substring(0, partID.Length - 2);
            }
            //Bindto Machine
            dtCm = BaseDAO.ExecuteQuery("Select [242_OptionData].OptionID, [242_OptionData].PartID, [242_OptionData].MachineID from [242_OptionData] where PartID = '" + partIDNew + "'");
            dtOp = BaseDAO.ExecuteQuery("Select [242_OptionData].OptionID, [242_OptionData].PartID, [242_OptionData].MachineID from [242_OptionData] where PartID = '" + partIDNew + "'");

            dtMo = BaseDAO.ExecuteQuery("Select [242_MO].Finishdate, [242_MO].Finished from [242_MO] where MONo='" + mONo + "'");
            if (dtMo.Rows.Count > 0)
            {
                try
                {
                    checkFinished.Checked = (bool)dtMo.Rows[0]["Finished"];
                    dateFinishDate.DateTime = (DateTime)dtMo.Rows[0]["Finishdate"];

                }
                catch (Exception)
                {

                    dateFinishDate.EditValue = null;
                    checkFinished.Checked = false;
                }

            }

            DataRow row = dtCm.NewRow();
            row["MachineID"] = string.Empty;
            row["OptionID"] = string.Empty;
            row["PartID"] = string.Empty;

            cbMachineID.DataSource = dtCm;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";

            //Bindto Optiondata
            cbOptionID.DataSource = dtOp;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            AddData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddData()
        {
            ClsMOOptionTable obj = new ClsMOOptionTable();
            if (GetData(obj))
            {

                //clspartDAO.InsertOptionData(obj);
                if (InsertData(obj) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.Product.MOOption;
                    log.Task = LogConstant.Task.Insert;
                    log.Description = "MONo: " + obj.MONo + "; PartID: " + obj.PartID + "; OptionID: " + obj.OptionID + "; Deadline: " + obj.Deadline;
                    log.WriteLog();

                    MessageBox.Show("Đã thêm mới xong");
                    //cbMachineID.Text=string.Empty;
                    //cbOptionID.Text=string.Empty;
                    //checkLastOption.Checked = false;
                    //txtProTime.Text=string.Empty;
                    //txtClampTime.Text = string.Empty;
                    //checkLastOption.Checked = false;
                    //checkFinished.Checked = false;
                    //dateFinishDate.EditValue = null;

                    //cbShift.SelectedText = string.Empty;
                    //txtJigType.Text = string.Empty;
                    //txtNote.Text = string.Empty;
                    cbMachineID.Focus();
                }
                else
                {
                    Utilities.MessageErrorOk("An error occurred while updating the record to database. Please try again.");
                }
            }
        }

        private bool GetData(ClsMOOptionTable obj)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtProTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ProTime.");
                txtProTime.Focus();
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtClampTime.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập ClampTime.");
                txtClampTime.Focus();
                return false;
            }

            if (float.Parse(txtMOQty.Text) < 0 || float.Parse(txtProTime.Text) < 0 || float.Parse(txtClampTime.Text) < 0)
            {
                MessageBox.Show("ProTime, ClampTime, AnodNote phải lớn hơn bằng 0.");
                txtMOQty.Focus();
                return false;

            }

            if (!cbOptionID.Text.Trim().Equals(string.Empty))
            {
                DataTable dtCheck =
                BaseDAO.ExecuteQuery("select * from [242_MOOption] where [MONo]='" + mONo + "' and [OptionID]='" +
                                     cbOptionID.Text + "'");
                if (dtCheck.Rows.Count > 0)
                {
                    MessageBox.Show("Đã có OptionID này trong bảng MOOption");
                    return false;
                }
            }
            else
            {
                MessageBox.Show("Hãy nhập OptionID");
                txtProTime.Focus();
                return false;
            }
            #endregion

            obj.MONo = cbMONo.Text;
            obj.PartID = cbPartID.Text;
            obj.MOQty = int.Parse(txtMOQty.Text);
            obj.Deadline = dateDeadline.DateTime;

            obj.MachineID = cbMachineID.Text;
            obj.OptionID = cbOptionID.Text;
            obj.LastOption = checkLastOption.Checked;
            obj.ProTime = float.Parse(txtProTime.Text);
            obj.ClampTime = float.Parse(txtClampTime.Text);
            obj.LastOption = checkLastOption.Checked;
            obj.Finished = checkFinished.Checked;
            obj.Finishdate = dateFinishDate.DateTime;

            obj.Shift = cbShift.Text;
            obj.JigID = txtJigType.Text;
            obj.Note = txtNote.Text;

            return true;
        }

        private int InsertData(ClsMOOptionTable obj)
        {
            string deadline = (DateTime.Compare(obj.Deadline, DateTime.Parse(minDate))) <= 0 ? "null" : "'" + obj.Deadline.ToString("MM/dd/yyyy") + "'";
            string finishdate = (DateTime.Compare(obj.Finishdate, DateTime.Parse(minDate))) <= 0 ? "null" : "'" + obj.Finishdate.ToString("MM/dd/yyyy") + "'";

            string sql = "insert into [242_MOOption]( " +
                         " [MONo],[PartID],[MOQty],[Deadline],[MachineID],[OptionID]" +
                         ",[LastOption],[JigType],[ProTime],[ClampTime],[Finished],[Finishdate],[Shift],[Note])" +
                         " values('" + obj.MONo + "', '" + obj.PartID + "', '" + obj.MOQty + "', " + deadline + ", '" + obj.MachineID + "'" +
                         ", '" + obj.OptionID + "', '" + obj.LastOption + "', '" + obj.JigID + "', " + obj.ProTime + ", " + obj.ClampTime +
                         ", '" + obj.Finished + "', " + finishdate + ", '" + obj.Shift + "', N'" + obj.Note + "'" +
                         " )";

            return BaseDAO.ExecuteNonQuery(sql);

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void frm_MOOptionAddThis_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btnEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btnEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                cbMachineID.Focus();
            }
        }

        private void txtProTime_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

    }
}